package com.itboye.api.domain;

import com.github.pagehelper.Page;
import com.itboye.api.model.ByResponseData;
import com.itboye.mybatis.helper.ByPageInfoHelper;
import com.itboye.mybatis.model.CompanyApplyModel;
import com.itboye.mybatis.model.CompanyEmployModel;
import com.itboye.mybatis.model.UserResumeModel;
import com.itboye.services.recruit.interfaces.ICompanyApplyService;
import com.itboye.services.recruit.interfaces.ICompanyEmployService;
import com.itboye.services.recruit.interfaces.IUserResumeService;
import com.itboye.system.exception.ByBaseException;
import com.itboye.system.exception.ByServiceException;
import com.itboye.system.helper.ByDateHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * CompanyEmployDomain.java
 * Created on  2016/7/29 10:26
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/7/29      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
@Component("CompanyEmployDomain")
public class CompanyEmployDomain extends BaseDomain {

    @Resource
    private ICompanyEmployService companyEmployService;

    @Resource
    private ICompanyApplyService companyApplyService;

    @Resource
    private IUserResumeService userResumeService;

    private UserResumeModel getUserResumeModel(Integer uid) throws ByServiceException {
        HashMap<String,String> hashMap = new HashMap<String, String>();
        hashMap.put("uid",uid.toString());
        return userResumeService.getInfoBy(hashMap);
    }

    /**
     * 应聘该岗位
     * @return
     */
    public ByResponseData apply() throws ByBaseException, UnsupportedEncodingException {
        this.checkApiVersion("100");

        //岗位ID
        Integer companyEmployId = this.getIntegerParam("id");
        //投用户UID
        Integer uid = this.getIntegerParam("uid");

        assertNotNull(uid,"缺少uid");
        assertNotNull(companyEmployId,"缺少id");

        CompanyEmployModel companyEmployModel = companyEmployService.getInfo(companyEmployId);

        if(companyEmployModel == null){
            return fail("该岗位不存在!");
        }

//      if(companyEmployModel.getValidStatus() == 0){
//          return fail("该信息未通过审核,不得投递!");
//      }

        UserResumeModel userResumeModel = getUserResumeModel(uid);
        if(userResumeModel == null){
            return fail("您没有简历可以投递,联系管理员添加自己的简历");
        }

        CompanyApplyModel companyApplyModel = new CompanyApplyModel();
        companyApplyModel.setCompanyId(0);
        companyApplyModel.setResumeId(userResumeModel.getUid());
        companyApplyModel.setResumeUid(uid);
        companyApplyModel.setCreateTime(ByDateHelper.getCurrentTimeStamp().intValue());
        companyApplyModel.setCompanyEmployId(companyEmployId);
        companyApplyModel.setUid(companyEmployModel.getUid());

        int lastInsertId = companyApplyService.insert(companyApplyModel);

        if(lastInsertId > 0){
            //TODO: 申请成功一次,则+1 申请数

            return success("投递成功");
        }

        return fail("投递失败!");
    }

    /**
     * 分页查询
     * 101. 增加is_auth_e 标志是否认证
     * 102. 增加logo,address 2个字段
     * @return
     * @throws ByBaseException
     * @throws UnsupportedEncodingException
     */
    public ByResponseData query() throws ByBaseException, UnsupportedEncodingException {
        this.checkApiVersion("103");

        String words = this.getStringParam("keywords");
        Integer pageNum  = this.getIntegerParam("page_num");
        Integer pageSize = this.getIntegerParam("page_size");
        assertNotNull(pageNum,"缺少page_num参数");
        assertNotNull(pageSize,"缺少page_size参数");

        HashMap<String,String> params = new HashMap<String, String>();

        params.put("validStatus","1");

        if(words != null && words.length() > 0){
            params.put("words",words);
        }

        String fields = "id,title,uid,create_time,update_time,valid_status,is_auth_e,logo,address";
        String orders = null;

        ByPageInfoHelper<CompanyEmployModel> employModelPage = companyEmployService.queryByPaging(params,pageNum,pageSize,fields, orders);

        return success(employModelPage);
    }

}
